<? include block('htmla');

if (!function_exists('showSelectableStruct')) {
	function showSelectableStruct($_root, $levels, $current, $data, $rid) {

		foreach($_root as $id => $v) {
			
			$class = array("level$current", $v['first'], $v['last']);

			?><li class="<?=implode(' ', array_trim_empty($class))?>"><?
			
			?><label><input type="checkbox" name="r<?=$rid?>[]" value="<?=$id?>" <?=is_array($data) ? (in_array($id, $data) ? 'checked="checked"' : '') : 'checked="checked"' ?> /><?
				?><?=$v['name']?><?
			?></label><?
			
			if ($levels > $current)
				if (isset($v['data']) && sizeof($v['data'])) {
					?><ul class="level<?=$current + 1?>"><? showSelectableStruct($v['data'], $levels, $current + 1, $data, $rid); ?></ul><?
				}
	
			?></li><?
		}
		
	}
}

startScript('jquery'); ?><script type="text/javascript">
jQuery(function() {
	$(".right").change(function(){
		var c = !!$(this).attr("checked"), d = $(this).parent().next().find(".detailed");
		d.attr("disabled", !c);
		if (!c) d.attr("checked", true).change();
	});
	$(".page .detailed:not(:checked)").parent().next().show();
	$(".page .detailed").change(function(){
		if ($(this).attr("checked"))
			$(this).parent().next().hide();
		else
			$(this).parent().next().show();
	});
	$(".page .struct input").change(function(){
		$(this).parent().parent().find("input").attr("checked", !!$(this).attr("checked"));
	});
})
</script><? endScript(); ?>

<form action="" method="post" enctype="multipart/form-data">

<fieldset><legend> = </legend>

		<div><label>Метка роли <br />
			<input type="text" name="name" maxlength="200" class="xwide" value="<?=h($DATA['object']['name'])?>" />
		</label></div>

		<div><label>
			<input type="checkbox" name="ro_def" value="<?=RO_DEFAULT?>" <?=($DATA['object']['attr'] & RO_DEFAULT) ? 'checked="checked"' : ''?> /> Доступна по умолчанию 
		</label></div>
		
		<div><label>Права доступа</label>
			
			<? if (sizeof($d = dat('objects'))) {
					
					$acl = dat('perms');

					?><div><?
					foreach($d as $id => $v) {
						switch($v['kind']) {
							case 0: // page
								?><div class="page"><?
//debug($acl[$id]['id_parent']);							
									?><label><?
										?><input class="right" type="checkbox" name="right[]" value="<?=$id?>" <?=$acl[$id] ? 'checked="checked"' : ''?> /><?
										?><?=$v['name']?><?
									?></label><?
	
									?><label><?
										?><input class="detailed" type="checkbox" name="rp[]" value="<?=$id?>" <?=is_array($acl[$id]['id_parent']) ? '' : 'checked' ?> <?=$acl[$id] ? '' : 'disabled' ?> /><?
										?>Применить ко всем страницам<?
									?></label><?
									
									?><fieldset class="struct"><legend>Права доступа постранично</legend><ul class="level1"><?
										showSelectableStruct($DATA['pages'], 10, 1, $acl[$id]['id_parent'], $id);
									?></ul></fieldset><?
								
								?></div><?
								break;

							case 1: // object
								?><div class="object"><?
									?><label><?
										?><input type="checkbox" name="right[]" value="<?=$id?>" <?=$acl[$id] ? 'checked="checked"' : ''?> /><?
										?><?=$v['name']?><?
									?></label><?
								?></div><?
								break;

						}
					}
					?></div><?

				} else {
					?><p class="diags">Права не определены.</p><?
			} ?>
			
		</div>
		


		<p><input type="submit" name="editor_save" value="Сохранить" /></p>

	</fieldset>

</form>

<? include block('_htmla') ?>